package com.woniuxy.user.service.impl;

import com.woniuxy.commons.entity.*;
import com.woniuxy.commons.exception.user.UserNotExitException;
import com.woniuxy.user.mapper.UserMapper;
import com.woniuxy.user.service.UserService;
import org.assertj.core.internal.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 吴毛伟
 * @date:2020/7/16 17:45
 * @description:
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public int getExam(int uid) {
        //首先去查询他有没有申请过考试
        int code = (Integer) userMapper.selectExam(uid);
        if (code == 1) {
            //正在考试
            return 2;
        }
        //加载考试信息
        return userMapper.getExam(uid);
    }

    @Override
    public int selectScore(int uid) {
        int score = userMapper.selectScore(uid);
        return score;
    }

    @Override
    public int insertMap(int uid, int id) {
        //首先查询有没有当前用户的学习地图记录信息
        LearnMap learnMap = userMapper.selectLearnMap(uid, id);
        if (learnMap != null) {
            //已存在不用添加
            return 1;
        }
        //如果不存在 则添加
        int row = userMapper.insertMap(uid, id);
        return 2;
    }

    @Override
    public User selectUserByUname(String uname) {
        User user = userMapper.selectUserByUname(uname);
        if (user == null) {
            throw new UserNotExitException();
        }
        return user;
    }

    @Override
    public CourseChapter findChapterByCid(int cid) {
        return userMapper.findChapterByCid(cid);
    }

    @Override
    public int insertAnswers(ExamAnswer examAnswer) {
        ExamAnswer examAnswers = userMapper.selectExamAnswer(examAnswer);
        if (examAnswers != null) {
            examAnswers.setAnswer(examAnswer.getAnswer());
            return userMapper.updateAnswer(examAnswers);
        }
        return userMapper.insertAnswers(examAnswer);
    }

    @Override
    public List<ExamAnswer> getAnswer(int examId, int uid) {
        return userMapper.getAnswer(examId, uid);
    }

    @Override
    public int insertByExamIdAndUserId(int paperId, int uid) {
        return userMapper.insertByExamIdAndUserId(paperId, uid);
    }

    @Override
    public ExamHistory findSatusByExamIdAndUserId(int paperId, int uid) {
        return userMapper.findSatusByExamIdAndUserId(paperId, uid);
    }

    @Override
    public int updateStatus1(int paperId, int uid) {
        return userMapper.updateStatus1(paperId, uid);
    }

    @Override
    public int updateStatus2(int paperId, int uid) {
        return userMapper.updateStatus2(paperId, uid);
    }

    @Override
    public List<User> selectAllUser() {
        return userMapper.selectAllUser();
    }
}
